package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuanwei.core.service.IXwLoginLogService;
import com.xuanwei.dao.entity.XwLoginLog;
import com.xuanwei.dao.mapper.XwLoginLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-04-23
 */
@Service
@Slf4j
public class XwLoginLogServiceImpl extends ServiceImpl<XwLoginLogMapper, XwLoginLog> implements IXwLoginLogService {

    @Override
    @Async
    public void add(XwLoginLog loginLog) {
        Document document = null;
        try {
            document = Jsoup.connect("http://www.ip138.com/ips138.asp?ip="+loginLog.getLoginIp()+"&action=2").get();
            String address = document.body().getElementsByTag("li").get(0).wholeText().replace("本站数据：","");
            loginLog.setLoginAddress(address);
        } catch (IOException e) {
            log.info(e.getMessage());
            e.printStackTrace();
        }
        getBaseMapper().insert(loginLog);
    }

    @Override
    public void list(Page<XwLoginLog> page) {
        getBaseMapper().selectPage(page,
                new QueryWrapper<XwLoginLog>()
                        .select("id,management_nickname,login_address,login_ip,login_time")
        .orderByDesc("login_time"));
    }

    @Override
    public void delete(List<Integer> ids) {
         getBaseMapper().deleteBatchIds(ids);
    }
}
